import pymongo

# 这个文件是独立运行的，不用scrapy命令，连接数据库，得到游戏的简要信息
mongo_client = pymongo.MongoClient("mongodb://user_spider:pq888888@192.168.136.128:27017/?authSource=spider")
# print(mongo_client)
mongo_db = mongo_client["spider"]
# print(mongo_db)
collist = mongo_db.list_collection_names()
# print(collist)
coll = mongo_db["MyabandonwareCategorfornameisnull"]
dict_firstSearch={}

str_firstSearch="THEME"
str_secondSearch="GENRE"

coll.create_index([("rate",-1)])#创建索引目的是为了减少资源的消耗，要不然下面的语句会崩
for n, i in enumerate(coll.find().sort([('rate', -1)])):#PLATFORM的一次统计
    if str_firstSearch in i:
        for j in i[str_firstSearch]:
            if j in dict_firstSearch:
                dict_firstSearch[j]=dict_firstSearch[j]+1
            else:
                dict_firstSearch[j]=1
for i in dict_firstSearch:
    tag_a="""<a onclick="myFunction()">%s(<span>%s</span>)</a>""" % (i,dict_firstSearch[i])
    with open("../../output/tag_a_forFirstSearch.html", encoding="utf-8", mode="a") as f:
        f.write(str(tag_a))

for i in dict_firstSearch:
    dict_secondSearch = {}
    for n,j in enumerate(coll.find({str_firstSearch:i}).sort([('rate',-1)])):
        if str_secondSearch in j:
            for k in j[str_secondSearch]:
                if k in dict_secondSearch:
                    dict_secondSearch[k]=dict_secondSearch[k]+1
                else:
                    dict_secondSearch[k]=1
    i=i.replace(" ","_")
    tag_2='<div class="item_2 %s">' %(i.upper())
    for l in dict_secondSearch:
        tag_2=tag_2+"""<a onclick="myFunction2()">%s(<span>%s</span>)</a>""" %(l,dict_secondSearch[l])
    tag_2=tag_2+"</div>"
    with open("../../output/tag_2_forFirstSearch.html", encoding="utf-8", mode="a") as f:
        f.write(str(tag_2))





